package com.share.example.selenium.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.share.common.common.Page;
import com.share.common.utils.HttpUtil;
import com.share.common.utils.MinioUtil;
import com.share.common.utils.PinYinUtil;
import com.share.example.selenium.mapper.JfUserMapper;
import com.share.example.selenium.model.db.JfUser;
import com.share.example.selenium.model.hzhc.user.HzhcUserPageReq;
import com.share.example.selenium.service.IJfUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-09-24
 */
@Service
public class JfUserServiceImpl extends ServiceImpl<JfUserMapper, JfUser> implements IJfUserService {

    @Resource
    private JfUserMapper jfUserMapper;

    @Override
    public Page pageList(HzhcUserPageReq req) {
        PageHelper.startPage(req.getPageNum(), req.getPageSize());
        LambdaQueryWrapper<JfUser> queryWrapper = new LambdaQueryWrapper();
        if (StringUtils.isNotBlank(req.getName())) {
            queryWrapper.like(JfUser::getName, req.getName());
        }
        if (StringUtils.isNotBlank(req.getPhone())) {
            queryWrapper.like(JfUser::getPhone, req.getPhone());
        }
        if (StringUtils.isNotBlank(req.getCompanyName())) {
            queryWrapper.like(JfUser::getCompanyName, req.getCompanyName());
        }
        if (StringUtils.isNotBlank(req.getRoleName())) {
            queryWrapper.like(JfUser::getRoleName, req.getRoleName());
        }
        if (StringUtils.isNotBlank(req.getStationName())) {
            queryWrapper.like(JfUser::getStationName, req.getStationName());
        }
        if (StringUtils.isNotBlank(req.getJobNumber())) {
            queryWrapper.like(JfUser::getJobNumber, req.getJobNumber());
        }
        if (StringUtils.isNotBlank(req.getSex())) {
            queryWrapper.eq(JfUser::getSex, req.getSex());
        }
        queryWrapper.orderByAsc(JfUser::getJobNumber);
        List<JfUser> list = jfUserMapper.selectList(queryWrapper);
        return new Page<>(list, new PageInfo<>(list).getTotal(), req);
    }

    //    @PostConstruct
    public void updateInit() {
        LambdaQueryWrapper<JfUser> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.isNotNull(JfUser::getImage);
        queryWrapper.isNotNull(JfUser::getName);
        queryWrapper.isNotNull(JfUser::getJobNumber);
        queryWrapper.isNull(JfUser::getLocalImage);
        List<JfUser> list = jfUserMapper.selectList(queryWrapper);
        for (JfUser user : list) {
            try {
                String image = user.getImage();
                if (image.length() < 3) {
                    continue;
                }

                String n1 = "_" + PinYinUtil.spellFirstChar(user.getName());
                String fileName = "/cimg/" + user.getJobNumber() + n1 + ".jpg";
                byte[] bytes = HttpUtil.downFile(image, null);
                boolean upLoad = false;
                if (bytes.length > 100) {
                    fileName = MinioUtil.uploadFile(fileName, bytes);
                    upLoad = true;
                }

                if (upLoad) {
                    JfUser record = new JfUser();
                    record.setId(user.getId());
                    record.setLocalImage(fileName);
                    record.setEmail(PinYinUtil.spell(user.getName()).replaceAll(" ", "") + "@ihanchen.com");
                    jfUserMapper.updateById(record);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
